﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FDSimpleModelGenerator.Common.Mvc
{
    public class CustomCookieAuthenticationEvents : CookieAuthenticationEvents
    {
        public CustomCookieAuthenticationEvents()
        {

        }

        public override async Task ValidatePrincipal(CookieValidatePrincipalContext context)
        {
            var userPrincipal = context.Principal;

            var lastChanged = (from c in userPrincipal.Claims where c.Type == "LastChanged" select c.Value)
                .FirstOrDefault();
            if (string.IsNullOrEmpty(lastChanged))
            {
                context.RejectPrincipal();

                await UserContext.Current.LogoutAsyn();
            }
        }
    }
}
